-
Notifications
You must be signed in to change notification settings - Fork 928
Issue 2393 - Add environment check to Analytics Module #3165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…e build (#3156) * Do not build firestore lite in build because it breaks release build * add release build script
Binary Size ReportAffected SDKs
Test Logs |
…into issue-2393-analytics
…into issue-2393-analytics
…into issue-2393-analytics
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, left some comments.
Assuming this is WIP and you will remove extra code comments and console logs in a later pass.
packages/analytics/index.ts
Outdated
@@ -45,6 +45,9 @@ declare global { | |||
* Type constant for Firebase Analytics. | |||
*/ | |||
const ANALYTICS_TYPE = 'analytics'; | |||
const NAMESPACE_EXPORTS = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is copied from messaging, but I don't think it's necessary here. In messaging, this was the top-level object passed to setServiceProps()
. Here, the top level object is a literal that currently contains settings
and EventName
, so you're adding another key named NAMESPACE_EXPORTS
which contains this nested object that contains isSupported()
. Have you manually tested calling the isSupported()
static method? Did this work?
packages/analytics/src/errors.ts
Outdated
[AnalyticsError.INDEXED_DB_UNSUPPORTED]: | ||
'IndexedDB is not supported by current browswer', | ||
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: | ||
"Environment doesn't support IndexedDB functionality with error message: {$errorInfo}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we need the words with error message
, just ...functionality: {$errorInfo}
packages/analytics/index.ts
Outdated
} | ||
interface FirebaseApp { | ||
analytics(): FirebaseAnalytics; | ||
} | ||
} | ||
function validateBrowserContext(): void { | ||
if ('indexedDB' in window && indexedDB !== null && navigator.cookieEnabled) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the cookieEnabled
a separate condition? I don't want to give a misleading error that indexedDB isn't available if it is available, but cookies are not.
packages/analytics/index.ts
Outdated
|
||
request.onerror = () => { | ||
throw ERROR_FACTORY.create(AnalyticsError.INVALID_INDEXED_DB_CONTEXT, { | ||
errorInfo: request.error!.message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any possibility there might not be an error or a message? would it be safer to go with request.error?.message || ''
just in case?
packages/analytics/index.ts
Outdated
throw ERROR_FACTORY.create(AnalyticsError.INDEXED_DB_UNSUPPORTED); | ||
} | ||
} | ||
function isSupported(): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably have jsdoc style comment explaining what this function does.
packages/analytics/index.ts
Outdated
} | ||
interface FirebaseApp { | ||
analytics(): FirebaseAnalytics; | ||
} | ||
} | ||
function validateBrowserContext(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably add jsdoc style comment explaining what this function does.
packages/firebase/index.d.ts
Outdated
@@ -5083,6 +5083,7 @@ declare namespace firebase.analytics { | |||
id?: string; | |||
name?: string; | |||
} | |||
function isSupported(): boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will need a comment here to document what this does. Comments in this file are the source of our official documentation, see other methods/properties for format.
…into issue-2393-analytics
…into issue-2393-analytics
…into issue-2393-analytics
…into issue-2393-analytics
…into issue-2393-analytics
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, thanks for all the changes and sorry for suggesting one more. The other comments are just grammar nits - "cookies are enabled/disabled" is usually how the functionality is referred to in communication even though I know the window property is cookieEnabled
.
packages/analytics/index.ts
Outdated
import { | ||
isIndexedDBAvailable, | ||
validateIndexedDBOpenable, | ||
isCookieEnabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: areCookiesEnabled
packages/analytics/src/errors.ts
Outdated
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: | ||
"Environment doesn't support IndexedDB: {$errorInfo}. Wrap initialization of analytics in analytics.isSupported() to prevent intialization in unsupported environments", | ||
[AnalyticsError.COOKIE_NOT_ENABLED]: | ||
'Cookie not enabled in this browser environment. Analytics requires cookies to be enabled.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "Cookies are not"
packages/analytics/src/errors.ts
Outdated
'IndexedDB is not supported by current browswer', | ||
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: | ||
"Environment doesn't support IndexedDB: {$errorInfo}. Wrap initialization of analytics in analytics.isSupported() to prevent intialization in unsupported environments", | ||
[AnalyticsError.COOKIE_NOT_ENABLED]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: COOKIES_NOT_ENABLED
packages/analytics/src/errors.ts
Outdated
[AnalyticsError.INDEXED_DB_UNSUPPORTED]: | ||
'IndexedDB is not supported by current browswer', | ||
[AnalyticsError.INVALID_INDEXED_DB_CONTEXT]: | ||
"Environment doesn't support IndexedDB: {$errorInfo}. Wrap initialization of analytics in analytics.isSupported() to prevent intialization in unsupported environments", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line wrap by breaking up string ("" + ""), and second "initialization" (after "prevent") missing a letter.
packages/analytics/src/helpers.ts
Outdated
* This method also validates browser context for indexedDB by opening a dummy indexedDB database and throws AnalyticsError.INVALID_INDEXED_DB_CONTEXT | ||
* if errors occur during the database open operation. | ||
*/ | ||
export async function validateInitializationEnvironment(): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, sorry for so many changes. Figuring this functionality out is kind of uncharted territory so there's been a lot of trying things out and undoing them if they don't work.
Since there's async and sync parts I think maybe we shouldn't have a single function for this anymore.
- I would suggest the first 2
if
blocks infactory()
just as they are here (since they can actually block initialization, why not let them?) - And then for the
validateIndexedDBOpenable()
check, call it with a chained.catch()
where you add the analytics-specific text and re-throw the error just as you do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks just about ready!
- One comment about maybe getting rid of eslint comment?
- Get rid of 2 stray auth files? (maybe came from a merge? checkout from origin/master)
- Change PR title to be descriptive of content: "Add environment check to analytics"?
packages/analytics/src/factory.ts
Outdated
} | ||
if (!isIndexedDBAvailable()) { | ||
throw ERROR_FACTORY.create(AnalyticsError.INDEXED_DB_UNSUPPORTED); | ||
} | ||
// Async but non-blocking. | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is eslint ok now if we get rid of this comment?
…into issue-2393-analytics
a fix for issue #2393 for analytics module.
Throw runtime error (UNSUPPORTED_BROWSER) on initialization of the instance when IndexedDB is not supported in some browser situations.
The fix is tested on Safari and Firefox private window rendering iframe with https://github.com/tomsun/firebase-js-sdk-securityerror-example provided in the issue conversation.